<meta charset='utf-8' />

<body style='padding:10px;color:#838'>

	<h2 style='text-align:center' class='title'>方剂查询</h2>

	<span style='margin-right:30px'>
		<input type="text" id='s_name' style='width:100px;margin-right:10px' placeholder='方名关键词'>
		<input type="text" id='s_fang' style='width:100px;margin-right:10px' placeholder='组方药关键词'>
		<input type="text" id='s_gong' style='width:100px;margin-right:10px' placeholder='功能关键词'>
		<img src="./public/search.png" id='searchBtn' style='cursor:pointer'>
	</span>


	<!--中药列表-->
	<div id='list' style='width:100%'></div>

	<div id='fpage' style='margin:30px 0'></div>

</body>

<script>
var pNow = 1;	//初始当前页
var pMax = 2;	//初始最大页

_init();

$('#searchBtn').click(function(){
	pNow = 1;
	pMax = 2;
	_init();
});



function _init(){
	Notice('加载中......');
	_getFangList(function(res){
		Notice();
		var tmp = _parseRes(res);
		if(tmp.length < 1){
			pMax = pNow;
			Fpage(pMax,pMax,function(p){
				pNow = p;
				pMax = p+1;
				_init();
			},1,1,'#fpage')
			$('#list').html('');
			return;
		}else{
			Fpage(pNow,pMax,function(p){
				pNow = p;
				pMax = p+1;
				_init();
			},1,1,'#fpage')
		}

		
		var str = '';
		for(var i=0;i<tmp.length;i++){
			str += "<div style='word-break:break-all;margin-top:20px;border-bottom:1px solid #ccc;padding-bottom:10px;font-size:13px'>";
				var c = " style='color:red'";

				var fang_name = $('#s_name').val().trim().split(' ');	//如果有关键字
				str += "<h3>方名："+_keyBold(tmp[i].name,fang_name)+"</h3>";

				str += "<div style='display:flex'>";
					str += "<div>方药：</div>";
					var fang_fang = $('#s_fang').val().trim().split(' ');
					str += "<div style='flex:1'>"+_keyBold(tmp[i].fang,fang_fang)+"</div>";
				str += "</div>";

				str += "<div style='display:flex'>";
					str += "<div>功能：</div>";
					var fang_gong = $('#s_gong').val().trim().split(' ');
					str += "<div style='flex:1'>"+_keyBold(tmp[i].gong,fang_gong)+"</div>";
				str += "</div>";
			str += "</div>";
		}
		$('#list').html(str);
		// if(tmp.length < 1){
		// 	pMax = pNow;
		// }
		// Fpage(pNow,pMax,function(p){
		// 	pNow = p;
		// 	pMax = p + 1;
		// 	_init();
		// },1,1,'#fpage')
	});
}

//获取中药列表
function _getFangList(fun){
	var data = {
		type:'_getFangList',
		name:$('#s_name').val().trim(),
		fang:$('#s_fang').val().trim(),
		gong:$('#s_gong').val().trim(),
		pNow:pNow
	}
	data.cacheName = md5(JSON.stringify(data));
	var path = './cache/'+data.cacheName;
	//请求缓存
	// Ajax({},function(res){		
	// 	if(/^\[.*\]$/.test(res)){
	// 		C('yao from cache');
	// 		fun(res)
	// 	}else{
	// 		//请求后台
	// 	}
	// },path);
			C('yao from after');
			Ajax2(data,function(res){
				fun(res);
			});

}
</script>